package site.sorghum.ddl.solon.config;

import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import site.sorghum.ddl.mybatis.MybatisDdlWrapper;
import site.sorghum.ddl.mybatis.MybatisEntityGenerate;
import site.sorghum.ddl.mybatis.MybatisInitConfig;
import site.sorghum.ddl.solon.prop.GlobalConfigProp;

import javax.sql.DataSource;

@Configuration
@org.springframework.context.annotation.Configuration
@Condition(onClass = MybatisInitConfig.class)
@ConditionalOnClass(value = MybatisInitConfig.class)
public class MybatisConfig {

    @Bean
    @org.springframework.context.annotation.Bean
    MybatisEntityGenerate mybatisEntityGenerate() {
        return new MybatisEntityGenerate();
    }


    @Bean
    @org.springframework.context.annotation.Bean
    MybatisDdlWrapper mybatisDdlWrapper(@Autowired @Inject DataSource dataSource) {
        return new MybatisDdlWrapper(dataSource);
    }


    @Bean
    @org.springframework.context.annotation.Bean
    MybatisInitConfig mybatisInitConfig(@Autowired(required = false) @Inject(required = false) GlobalConfigProp globalConfig,
                                        @Autowired @Inject MybatisDdlWrapper mybatisDdlWrapper, @Autowired @Inject MybatisEntityGenerate mybatisEntityGenerate) {
        if (globalConfig != null) {
            globalConfig.init();
        }
        return new MybatisInitConfig(mybatisDdlWrapper, mybatisEntityGenerate);
    }
}
